"""--------------------------------------------------------------------------------------------------
Filename:   dialogs.py
Author:     Chris Chester, adapted from work by Ben Han
Date:       08-02-2013
Description:
    Contains classes to display menus or dialogs.
    
--------------------------------------------------------------------------------------------------"""

import wx
import time
import splashLogo
      

      
#===============================================================================
#    CLASS: BreakDialog
#    Creates a frame for the break timer
#===============================================================================
class BreakDialog(wx.Frame):
    """ This class implements a basic timer with an elapsed time display.
        The constructor takes a minimum of two arguments:
            - a callable object to be run when the timer reaches the specified time
            - the length of time before running callable object (seconds)
        This
        is done when the button is clicked, and also when the window is closed. So
        every case leads to the execution of the _end_timer() function, which
        closes the window.
    """
    def __init__(self, on_timer, period, parent=None, id=-1):
        
        '''
        ==============  =====================================
        **Arguments:**
        on_time           (function) What to do when time is up
        period            (int) Break length in seconds
        parent            (wx.frame) Window to use as the main
                          window
        ID                (int) ID for window

        ==============  =====================================
       '''
        
        wx.Frame.__init__(self, parent, id, title='Take a break :-)', size=(300, 150))
        self.on_timer = on_timer
        self._break_len = period
        
        self.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.BOLD))
        panel = wx.Panel(self, -1)
        self.break_button = wx.Button(panel, -1, 'Continue trials', size=(150, 30))
        self.time_display = wx.TextCtrl(panel, -1, size=(200, 30), style=wx.TE_CENTRE)
        self.time_display.SetEditable(False)
        
        box = wx.StaticBox(panel, -1, "Break Timer")
        sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
        sizer.Add(self.time_display, flag=wx.ALIGN_CENTER | wx.ALL, border=5)
        sizer.Add(self.break_button, flag=wx.ALIGN_CENTER | wx.ALL, border=5)
        panel.SetSizer(sizer)

        self._update_time(None)
        self.timer = wx.Timer(self, -1)
        self.timer.Start(1000)      # update timer every second (1000ms)
        self.Bind(wx.EVT_TIMER, self._update_time)
        self.Bind(wx.EVT_CLOSE, self._on_close)
        self.Bind(wx.EVT_BUTTON, self._end_timer, self.break_button)
        self.Centre()
        self.MakeModal(True)        # must be set to False before closing this window
        self.Show()
        
    def _update_time(self, event):
        '''
        Decrement timer
        '''
        mins = 0
        secs = self._break_len
        if self._break_len >= 60:
            mins = int(self._break_len / 60)
            secs = self._break_len - (60 * mins)
        if secs < 10:
            secs = '0' + str(secs)
        time_str = '%i:%s' % (mins, secs)
        self.time_display.SetValue(time_str)
        self.Refresh()
        if self._break_len <= 0:
            self._end_timer(None)
        self._break_len -= 1

    def _end_timer(self, event):
        self.timer.Stop()
        self.on_timer()
        self.MakeModal(False)
        self.Destroy()
        
    def _on_close(self, event):
        self._end_timer(event)
       


#===============================================================================
# CLASS: AboutDialog
##    Dialog displaying information about the application
#===============================================================================
class AboutDialog(wx.MessageDialog):        
    def __init__(self, parent):
        about_text = """Biofeedback in Swallowing Skill Training (BiSSkiT)
Version:    1.7
Released:  08 February 2013
This application was designed for use with the NeuroTrac Simplex sEMG Biofeedback device (Verity Medical, Ltd., Hampshire, UK)
 
Authorship:
Protocol Concept and Development: Maggie-Lee Huckabee PhD, Oshrat Sella PhD, Richard Jones PhD
Software Programming:  Ben Han, Christopher Chester
Manual:  Rowena Woolgar, Maggie-Lee Huckabee PhD
The University of Canterbury Swallowing Rehabilitation Research Laboratory
"""
        wx.MessageDialog.__init__(self, parent,
                                  about_text,
                                  caption="About", 
                                  style=wx.OK|wx.CENTRE)
        
        self.ShowModal()



#===============================================================================
# CLASS: StartDialog
##    A splash screen to be displayed before launching the application.
#===============================================================================
class StartDialog(wx.SplashScreen):
    """
    Create a splash screen widget.
    """
    def __init__(self, parent=None):
        logo = splashLogo.getsplashBitmap()
        splashStyle = wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT
        splashDuration = 1000 # milliseconds
        # Call the constructor with the above arguments in exactly the
        # following order.
        wx.SplashScreen.__init__(self, logo, splashStyle, splashDuration, parent)
        wx.Yield()
# End of dialogs.py